<template>
  <div class="login-box">
    <el-card>
      <template #header>面经登录</template>
      <el-form
        ref="form"
        label-position="top"
        label-width="60px"
        :model="form"
        :rules="rules"
        @keyup.enter.native="login"
      >
        <el-form-item label="用户名:" prop="username">
          <el-input
            placeholder="请输入用户名"
            v-model="form.username"
            @keyup.enter.native="fn"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="form.password"
            type="password"
            ref="psw"
          ></el-input>
        </el-form-item>
        <el-form-item class="login-btn">
          <el-button type="primary" @click="login" :loading="flag"
            >登录</el-button
          >
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { setLogin } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 5,
            max: 11,
            message: '长度必须为5~11个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\w{5,11}$/,
            message: '密码应为数字、字母、下划线,长度必须为5~11个字符',
            trigger: ['blur', 'change']
          }
        ]
      },
      flag: false
    }
  },
  methods: {
    async login() {
      try {
        await this.$refs.form.validate()
        this.flag = true
        const res = await setLogin(this.form)
        // console.log(res)
        this.$store.commit('user/updateToken', res.data.token)
        setToken(res.data.token)
        this.$router.push('/')
      } catch (error) {
        console.log(error)
        if (error.response && error.response.status === 400) {
          this.$message.error('用户名或密码错误')
        } else {
          this.$message.error('表单验证失败，无法登录！')
        }
      } finally {
        this.flag = false
      }
    },
    reset() {
      this.$refs.form.resetFields()
    },
    fn() {
      this.$refs.psw.focus()
    }
  },
  components: {}
}
</script>

<style scoped lang="scss">
.login-box {
  width: 100vw;
  height: 100vh;
  background: url('@/assets/login-bg.svg') no-repeat center / cover;
  display: flex;
  justify-content: center;
  align-items: center;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      text-align: center;
      color: #fff;
      font-size: 22px;
      background-color: rgba(114, 124, 245, 1);
    }
    .login-btn {
      text-align: center;
      .el-button:first-child {
        background-color: rgba(114, 124, 245, 1);
        border-color: rgba(114, 124, 245, 1);
      }
    }
  }
}
</style>
